package ejb.cbm.admin.session;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Service;

import ejb.cbm.admin.entity.TipoParametroEntity;
import ejb.cbm.admin.local.TipoParametroSessionLocal;

////@Stateless
@Service("tipoParametroSessionLocal")
public class TipoParametroSession  extends HibernateDaoSupport implements TipoParametroSessionLocal {
	
	static Logger logger = Logger.getLogger(TipoParametroSession.class.getName());
	//property constants
	public static final String CODIGOUNICO = "codigounico";
	public static final String TIPO = "tipo";
	public static final String DESCRIPCIONPARAMETRO = "descripcionparametro";
	public static final String FLAGACTIVO = "flagactivo";
	public static final String USUARIOREGISTRO = "usuarioregistro";
	public static final String USUARIOMODIFICACION = "usuariomodificacion";

	//private HibernateTemplate hibernateTemplate;

	@Autowired
	public TipoParametroSession(SessionFactory sessionFactory){
		super.setSessionFactory(sessionFactory);
	}
	

    //@PersistenceContext
	/*
	private EntityManager entityManager;
	
    public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
		//sessionFactory.
	}*/

	public void save(TipoParametroEntity transientInstance) {
    	logger.info("saving TipoParametroEntity instance");
        try {
        	getSession().persist(transientInstance);
            logger.info("save successful");
        } catch (RuntimeException re) {
        	logger.fatal("save failed");
            throw re;
        }
    }
    
    public void delete(TipoParametroEntity persistentInstance) {
    	logger.info("deleting TipoParametroEntity instance");
        try {
        	getSession().delete(persistentInstance);
            logger.info("delete successful");
        } catch (RuntimeException re) {
        	logger.fatal("delete failed");
            throw re;
        }
    }
    
    public TipoParametroEntity update(TipoParametroEntity detachedInstance) {
    	logger.info("updating TipoParametroEntity instance");
        try {
            TipoParametroEntity result = (TipoParametroEntity) getSession().merge(detachedInstance);
            logger.info("update successful");
            return result;
        } catch (RuntimeException re) {
        	logger.fatal("update failed");
            throw re;
        }
    }
    
    public TipoParametroEntity findById(Long id) {
		logger.info("findById TipoParametroEntity instance");		
		try {
            TipoParametroEntity instance = (TipoParametroEntity) getSession().get(TipoParametroEntity.class, id);
            return instance;
        } catch (RuntimeException re) {
        	logger.fatal("find failed");
            throw re;
        }
    }
	
    //gclv 21/09/2011
	//@SuppressWarnings("unchecked")
	public List<TipoParametroEntity> findByFiltro(TipoParametroEntity filtro){
		logger.info("findByFiltro TipoParametroEntity instance");
		StringBuilder query = new StringBuilder();
		
		try{
			query.append("select model ");
			query.append("from  TipoParametroEntity model ");
			query.append("where 1 = 1 ");
			
			if (filtro.getCodigotipoparametro()!=null){
				query.append(" and model.codigotipoparametro = "+filtro.getCodigotipoparametro());
			}
			if (filtro.getCodigounico()!=null){
				query.append(" and model.codigounico = '"+filtro.getCodigounico()+"'");
			}
			if (filtro.getTipo()!=null){
				query.append(" and model.tipo = "+filtro.getTipo());
			}
			if (filtro.getDescripcionparametro()!=null){
				query.append(" and model.descripcionparametro = '"+filtro.getDescripcionparametro()+"'");
			}
			if (filtro.getFlagactivo()!=null){
				query.append(" and model.flagactivo = "+filtro.getFlagactivo());
			}
			query.append(" order by model.codigotipoparametro ");
			
			return getSession().createQuery(query.toString()).list();
			
		}catch(RuntimeException e){
			logger.fatal("findByFiltro failed");
			throw e;
		}
	}
    
    //@SuppressWarnings("unchecked")
    public List<TipoParametroEntity> findByProperty(String propertyName, Object value) {
    	logger.info("finding TipoParametroEntity instance with property: " + propertyName
    						+ ", value: " + value);
		try {
			String queryString = "select model from TipoParametroEntity model where model." 
			 						+ propertyName + "= :propertyValue";
			return getSession().createQuery(queryString)
 				.setParameter("propertyValue", value).list();
		} catch (RuntimeException re) {
			logger.fatal("find by property name failed");
			throw re;
		}
	}
	
	public List<TipoParametroEntity> findByCodigounico(Object codigounico) {
		return findByProperty(CODIGOUNICO, codigounico);
	}
	
	public List<TipoParametroEntity> findByTipo(Object tipo) {
		return findByProperty(TIPO, tipo);
	}
	
	public List<TipoParametroEntity> findByDescripcionparametro(Object descripcionparametro) {
		return findByProperty(DESCRIPCIONPARAMETRO, descripcionparametro);
	}
	
	public List<TipoParametroEntity> findByFlagactivo(Object flagactivo) {
		return findByProperty(FLAGACTIVO, flagactivo);
	}
	
	public List<TipoParametroEntity> findByUsuarioregistro(Object usuarioregistro) {
		return findByProperty(USUARIOREGISTRO, usuarioregistro);
	}
	
	public List<TipoParametroEntity> findByUsuariomodificacion(Object usuariomodificacion) {
		return findByProperty(USUARIOMODIFICACION, usuariomodificacion);
	}
	

	//@SuppressWarnings("unchecked")
	public List<TipoParametroEntity> findAll() {
		logger.info("finding all TipoParametroEntity instances");
		try {
			String queryString = "select model from TipoParametroEntity model";
			return getSession().createQuery(queryString).list();
		} catch (RuntimeException re) {
			logger.fatal("find all failed");
			throw re;
		}
	}
}